/**
 * Created by zhanglihe on 15/11/10.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'calendar'
], function ($, common, ready, Collection, template, Model, dialog, calendar) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'data_report',
                condition: {limit: 50, offset: 0, level: 1},
                model: {
                    url:_global.url.api + 'data_report',
                    event: {
                        '.item-edit:click': 'edit'
                    },
                    showSql: function (e) {
                        e.preventDefault();
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '数据Sql',
                            content: this.data.sql,
                            width: 600,
                            ok: function () {
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();


                    },edit: function(e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title:'修改笔记',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function() {
                                _this.data.name = $('#name').val();

                                if(_this.data.name==''){
                                    Alert({msg:'请填写标题',type:'error'});
                                    return false;
                                }
                                _this.data.intro = $('#intro').val();

                                if(_this.data.intro==''){
                                    Alert({msg:'请填写备注',type:'error'});
                                    return false;
                                }
                                _this.data.id = _this.data.id;
                                if(model.put()){
                                    _this.collection.reload();
                                };
                            },cancel: function() {}
                        });
                        _this.dialog.show();
                        $('input,select,textarea').on('change', function(e) {
                            _this.change(e);
                        });
                    }

                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            var _this = this;
            //日期
            $('.long_date').calendar();

            //查询
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                var start_time = $('#start_time').val();
                var end_time = $('#end_time').val();
                var trace_index = $("select[name='trace_index'] option:selected").val();
                var source = $("select[name='app_source'] option:selected").val();
                var type = $("#btn-search").attr('type');
                var ids = new Array();
                $("input[name='data_report']:checked").each(function () {
                    ids.push($(this).val());
                })
                ids = ids.join(',');
                ids ? ids : ids = '@' + trace_index;
                if ((ids == '' || ids == undefined || ids == '@0') && (trace_index == 0)) {
                    Alert({msg: '请选择要查看的项', type: 'error'});
                    return false;
                }

                if (start_time == '' || start_time == 'undefined') {
                    Alert({msg: '请填写查询开始时间', type: 'error'});
                    return false;
                }
                if (end_time == '' || end_time == 'undefined') {
                    Alert({msg: '请填写查询结束时间', type: 'error'});
                    return false;
                }
                if (trace_index != 0) {
                    $.ajax({
                        url: '/api/data_report_info_detail?ids=' + ids + '&type=' + type + '&start_time=' + start_time + '&end_time=' + end_time + '&source=' + source,
                        type: 'get',
                        success: function (res) {
                            if (res.code < 0) {
                                alert(res.msg);
                                return false;
                            } else {
                                location.href = '/admin/see_data/' + ids + '/' + type + '/' + start_time + '/' + end_time + '/' + source;
                            }
                        }
                    });
                } else {
                    location.href = '/admin/see_data/' + ids + '/' + type + '/' + start_time + '/' + end_time + '/' + source;
                }

            });

            $('#btn-report').on('click', function (e) {
                e.preventDefault();
                var start_time = $('#start_time').val();
                var end_time = $('#end_time').val();
                var trace_index = $("select[name='trace_index'] option:selected").val();
                var source = $("select[name='app_source'] option:selected").val();
                var type = $("#btn-search").attr('type');
                var report=1;
                var ids = new Array();
                $("input[name='data_report']:checked").each(function () {
                    ids.push($(this).val());
                })
                ids = ids.join(',');
                ids ? ids : ids = '@' + trace_index;
                if ((ids == '' || ids == undefined || ids == '@0') && (trace_index == 0)) {
                    Alert({msg: '请选择要查看的项', type: 'error'});
                    return false;
                }

                if (start_time == '' || start_time == 'undefined') {
                    Alert({msg: '请填写查询开始时间', type: 'error'});
                    return false;
                }
                if (end_time == '' || end_time == 'undefined') {
                    Alert({msg: '请填写查询结束时间', type: 'error'});
                    return false;
                }
                if (trace_index != 0) {
                    $.ajax({
                        url: '/api/data_report_info_detail?ids=' + ids + '&type=' + type + '&start_time=' + start_time + '&end_time=' + end_time + '&source=' + source+ '&report=' + report,
                        type: 'get',
                        success: function (res) {
                            if (res.code < 0) {
                                alert(res.msg);
                                return false;
                            } else {
                                location.href = '/admin/see_datas/' + ids + '/' + type + '/' + start_time + '/' + end_time + '/' + source+'/'+report;
                            }
                        }
                    });
                } else {
                    location.href = '/admin/see_datas/' + ids + '/' + type + '/' + start_time + '/' + end_time + '/' + source+'/'+report;
                }

            })

            //处理不能按小时数据查询
            $("select[name='x']").change(function (e) {
                _this.collection.condition.type = $("select[name='x'] option:selected").val();
                _this.collection.reload();
            })

            //$('.quick_time').on('click', function (e) {
            //    e.preventDefault();
            //    var times = $(this).attr('rel');
            //    var start_time, end_time;
            //    var curDate = new Date();
            //    if (times == 72) {
            //        start_time = _this.formatTime(new Date(curDate.getTime() - 72 * 60 * 60 * 1000));
            //    } else if (times == 24) {
            //        start_time = _this.formatTime(new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), 0, 0, 0));
            //    }
            //    end_time = _this.formatTime(new Date());
            //    $('#start_time').val(start_time);
            //    $('#end_time').val(end_time);
            //});
            var start_time, end_time;
            var curDate = new Date();
            start_time = _this.formatTime(new Date(curDate.getTime() - 14 * 24  * 60 * 60 * 1000));
            //start_time = _this.formatTime(new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), 0, 0, 0));
            end_time = _this.formatTime(new Date());
            $('#start_time').val(start_time);
            $('#end_time').val(end_time);
        },
        formatTime: function (date) {
            var month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
            var curDay = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            return date.getFullYear() + '-' + month + '-' + curDay;
        }

    };

    $(function () {
        new index();
    });
});